window.onload = function () {
    document.getElementById("administrative").className = "add-nav-active";
    document.getElementById("received-document").className = "add_class";
    // document.getElementById("organizational_management_ul").style.display = "inline-block";
    document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
    station_list();
};



function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        // document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        // document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("meeting_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("meeting_management_ul").style.display = "none";
    }
}

function click_ul3() {
    if (document.getElementById("jiantou3").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-xia";
        document.getElementById("issue_approval_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-shang";
        document.getElementById("issue_approval_ul").style.display = "none";
    }
}


// 加载待办列表
function station_list(page) {
     page = page || 1;
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/received_document.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
                document.getElementById("loading").style.display = "none";
            }
        }
    };
}

function create_table(count_num, json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var td1 = document.createElement("td");
        var td2 = document.createElement("td");
        var td3 = document.createElement("td");
        var td4 = document.createElement("td");
        var td5 = document.createElement("td");
        var td6 = document.createElement("td");
        var td7 = document.createElement("td");
        if(json_data[i]["type"] === '内部发文'){
            td.innerHTML = json_data[i]["post_name"];td.title = json_data[i]["post_name"];td.style.textAlign = "center";
            td1.innerHTML = json_data[i]["title"];td1.title = json_data[i]["title"];td1.style.paddingLeft = "1%;";
            td2.innerHTML = "<a href='"+json_data[i]["main_body"]+"'>"+json_data[i]["filename"]+"</a>";td2.title = json_data[i]["filename"];td2.style.textAlign = "center";
            td5.innerHTML = json_data[i]["is_the_date"];td5.title = json_data[i]["is_the_date"];td5.style.textAlign = "center";

            // td6.innerHTML = '<p onclick="dowload_pdf(\''+String(json_data[i]["main_body"])+'\')">下载</p>';td6.style.textAlign = "center";
            // "<a href='/administrative/examine.html?reception=true&id="+json_data[i]["id"]+"'>"+"查看"+"</a>"+" | " + "<a href='#' onclick='received("+json_data[i]["id"]+")'>"+"收阅"+"</a>";
            td6.innerHTML ="<a href='/administrative/examine.html?reception=true&id="+json_data[i]["id"]+"'>"+"查看"+"</a>"+" | " + '<a href="dowload_pdf?file_path='+json_data[i]["main_body"]+'">下载</a>'+" | "+"<a href='#' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" onclick=\"entrust(1, "+json_data[i]["id"]+")\">"+"转阅"+"</a>";td6.style.textAlign = "center";
        }else {
            td2.innerHTML = "<a href='"+json_data[i]["already_href"]+"'>"+json_data[i]["file_attachments"]+"</a>";td2.title = json_data[i]["file_attachments"];td2.style.textAlign = "center";

             td3.innerHTML = json_data[i]["Jeremy_levin_unit"];td3.title = json_data[i]["Jeremy_levin_unit"];td3.style.textAlign = "center";
             td4.innerHTML = json_data[i]["the_Titanic"];td4.title = json_data[i]["the_Titanic"];td4.style.textAlign = "center";
             td6.innerHTML = "<a href='/administrative/addressee_examine.html?REceived=true&id="+json_data[i]["id"]+"'>查看</a>" + " | " + "<a href='dowload_pdf?file_path="+json_data[i]["already_href"]+"'>下载</a>"+" | "+"<a href='#' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" onclick=\"entrust(2, "+json_data[i]["id"]+")\">"+"转阅"+"</a>";td6.style.textAlign="center";

        }

        td7.innerHTML = json_data[i]["type"];td7.title = json_data[i]["type"];td7.style.textAlign = "center";
        console.log('td7', td7.innerHTML)
        tr.appendChild(td7);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td5);tr.appendChild(td6);
        t_table.appendChild(tr);
    }

}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

function dowload_pdf(file_path) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('GET', "/administrative/dowload_pdf?file_path="+String(file_path), true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {

            }
        }
    };
}


function pagenum(dataTotals, page) {
     page = page || 1;
     dataTotals = dataTotals || 0;
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            station_list(sent_str='', page);
            document.getElementById("check_all").checked = false;
        }
    });
}

